var express = require('express')
var cors = require('cors')
//引入中间件
const bodyParser = require('body-parser')

var adminRouter = require('./router/adminRouter.js')
var stuRouter = require('./router/stuRouter.js')
//2、创建web服务器  app接收
var app = express()



app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', 'X-Requested-With')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  res.header('X-Powered-By', '3.2.1')
  res.header('Content-Type', 'application/json;charset=utf-8')
  next() //执行下一个中间件
})


// 请求体解析中间件
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))


//路由已经放到routes.js中
app.use(adminRouter)
app.use(stuRouter)

//4、启动服务listen(端口号,回调函数)
app.listen(8000, () => {
  console.log('服务器开启 http://127.0.0.1:8000')
})
